﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using WebBrowserTest.Utility;

namespace WebBrowserTest.DetailsAnalyzing
{
    public class YahooBrowseAnalyzer
    {
        private string _pageHtml = string.Empty;
        private HtmlDocument _htmlDoc = new HtmlDocument();
        private List<string> _domains = new List<string>();
        private YahooBrowseAnalyzer()
        {
        }

        public YahooBrowseAnalyzer(string url)
        {
            string yahooLink = Utils.GetUrlFromConfiguration("backlinkUrl");
            _pageHtml = Utils.getPageHtmlString(string.Format(yahooLink,url));
            _htmlDoc.LoadHtml(_pageHtml);
        }

        public List<string> GetDomains(int bldNumber)
        {
            var links = _htmlDoc.DocumentNode.SelectNodes("//span[@class='result']");
            if(links == null) return new List<string>();
            var domainList = links.Select(link => link.SelectSingleNode("a").Attributes["href"] != null ? link.SelectSingleNode("a").Attributes["href"].Value ?? "" : string.Empty).Take(bldNumber).ToList();
            domainList.RemoveAll(o => string.IsNullOrEmpty(o));
            return domainList;
        }
    }
}
